package com.bkck.sharkblockade.levels.explosion;

import com.bkck.sharkblockade.levels.block.Block;
import android.graphics.Bitmap;
import android.graphics.Canvas;

/**
 * The explosion class.
 * 
 * @author Team BKCK
 * 
 */
public class Explosion {

  // an array of bitmap images to draw for explosion.
  private Bitmap[] images;

  // the coordinate.
  private float x;
  private float y;

  // the dimension
  private int width;
  private int height;

  // in milliseconds
  private int frameDuration = 100;

  // variables needed to check the actual frame duration.
  private long lastUpdateTime = -1;
  private long elapsedTime = 0;

  // the current image
  private int currentImage;

  // whether there's an explosion
  private boolean exploding;

  // whether the explosion is increasing or decreasing size.
  private boolean fading;

  /**
   * The constructor.
   * 
   * @param images The array of bitmap images for explosion.
   */
  public Explosion(Bitmap[] images) {
    this.images = images;
    this.width = images[0].getWidth();
    this.height = images[0].getHeight();
    this.x = 0;
    this.y = 0;
    
    // initialize currentImage to 0 to let the explosion play from frame 0 to 3 and back to 0
    // initialize currentImage to 3 to let the explosion play from frame 3 to 0 only
    // note that if we change the value here we also need to match the value in method endExplosion.
    this.currentImage = 3;
    this.exploding = false;
    this.fading = false;
  }

  /**
   * Whether it's exploding.
   * @return exploding
   */
  public boolean isExploding() {
    return exploding;
  }

  /**
   * Start the explosion animation.
   * @param bombBlock The bomb block.
   */
  public void startExplosion(Block bombBlock) {
    x = bombBlock.getX() - (width - bombBlock.getWidth()) / 2;
    y = bombBlock.getY() - (height - bombBlock.getHeight()) / 2;
    exploding = true;
  }

  /**
   * Updates the explosion.
   */
  public void update() {

    // if there's no explosion, no need to update
    if (!exploding) {
      return;
    }

    // update the time variables
    long currentTime = System.currentTimeMillis();
    if (lastUpdateTime < 0) {
      lastUpdateTime = currentTime;
      return;
    }

    elapsedTime = elapsedTime + currentTime - lastUpdateTime;
    lastUpdateTime = currentTime;

    // end explosion after all frames were displayed once.
    if (currentImage == 0 && elapsedTime >= frameDuration && fading) {
      endExplosion();
      return;
    }

    // change fading to true to draw the frames backward
    if (currentImage == 3) {
      fading = true;
    }

    // update the currentImage
    if (elapsedTime < frameDuration) {
      return;
    }
    else {
      elapsedTime = 0;
      if (fading) {
        currentImage--;
      }
      else {
        currentImage++;
      }
    }
  }

  /**
   * End the explosion.
   */
  public void endExplosion() {
    currentImage = 3;
    exploding = false;
    fading = false;
    lastUpdateTime = -1;
  }

  /**
   * Draw the explosion on the canvas.
   * @param canvas The canvas.
   */
  public void draw(Canvas canvas) {
    if (exploding) {
      canvas.drawBitmap(images[currentImage], x, y, null);
    }
  }

}
